package com.far;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;


public class FlashySquare {

	private float vertices[] = {-1.0f, 1.0f, 0.0f,
								-1.0f, -1.0f, 0.0f,
								1.0f, -1.0f, 0.0f,
								1.0f, 1.0f, 0.0f
								};
	
	float[] colours = {
            1f, 0f, 0f, 1f, // vertex 0 Red
            0f, 1f, 0f, 1f, // vertex 1 Green
            0f, 0f, 1f, 1f, // vertex 2 Blue
            1f, 0f, 1f, 1f, // vertex 3 Purple
    };

	private short[] indices = {0, 1, 2, 0, 2, 3};
	private FloatBuffer vertexBuffer;
	private ShortBuffer indexBuffer;
	private FloatBuffer colourBuffer;
	
	public FlashySquare(){
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);
		
		// float has 4 bytes, colours (RGBA) * 4 bytes
		ByteBuffer cbb = ByteBuffer.allocateDirect(colours.length * 4);
		cbb.order(ByteOrder.nativeOrder());
		colourBuffer = cbb.asFloatBuffer();
		colourBuffer.put(colours);
		colourBuffer.position(0);
	}
	
	
	public void draw(GL10 gl){
		gl.glColor4f(0.1f, 0.1f, 1.0f, 1.0f); //RBGA
		gl.glFrontFace(GL10.GL_CCW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colourBuffer);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
		
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
		
	}

}
